﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Core;
using Domain.MainModule.Entities;
using Domain.Core.Specification;
using Domain.MainModule.CustomersMTS;

namespace Application.MainModule.CustomersMTSManagement
{
    public class CustomerMTSManagementService : ICustomerMTSManagementService, IDisposable
    {
        #region Members

        ICustomerMTSRepository _CustomerMTSRepository;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor for CustomerMTSService
        /// </summary>
        /// <param name="customerMTSRepository">CustomerMTSRepository dependency, usually this is resolver with IoC</param>
        public CustomerMTSManagementService(ICustomerMTSRepository customerMTSRepository)
        {
            if (customerMTSRepository == (ICustomerMTSRepository)null)
                throw new ArgumentNullException(Resources.Messages.exception_DependenciesAreNotInitialized);

            _CustomerMTSRepository = customerMTSRepository;
        }

        #endregion

        public void AddCustomerMTS(CustomerMTS customerMTS)
        {
            IUnitOfWork unitOfWork = _CustomerMTSRepository.UnitOfWork as IUnitOfWork;
            _CustomerMTSRepository.Add(customerMTS);
            unitOfWork.Commit();
        }

        public void ChangeCustomerMTS(CustomerMTS customerMTS)
        {
            IUnitOfWork unitOfWork = _CustomerMTSRepository.UnitOfWork as IUnitOfWork;
            _CustomerMTSRepository.Modify(customerMTS);
            unitOfWork.CommitAndRefreshChanges();
        }

        public void RemoveCustomerMTS(CustomerMTS customerMTS)
        {
            if (customerMTS == (CustomerMTS)null)
                throw new ArgumentNullException("customerMTS");

            IUnitOfWork unitOfWork = _CustomerMTSRepository.UnitOfWork as IUnitOfWork;
            customerMTS.IsEnabled = false;
            _CustomerMTSRepository.Modify(customerMTS);
            unitOfWork.CommitAndRefreshChanges();
        }

        public CustomerMTS FindCustomerMTSByName(string firstName, string lastName, string firstSurname, string lastSurname)
        {
            CustomerMTSNameSpecification spec = new CustomerMTSNameSpecification(firstName, lastName, firstSurname, lastSurname);
            return _CustomerMTSRepository.FindCustomerMTS(spec);
        }

        public CustomerMTS FindCustomerMTSById(Guid userId)
        {
            CustomerMTSUserIdSpecification spec = new CustomerMTSUserIdSpecification(userId);
            return _CustomerMTSRepository.FindCustomerMTS(spec);
        }

        #region IDisposable

        /// <summary>
        /// Dispose associated context with this domain service
        /// </summary>
        public void Dispose()
        {
            if (_CustomerMTSRepository != null
                &&
                _CustomerMTSRepository.UnitOfWork != null)
            {
                _CustomerMTSRepository.UnitOfWork.Dispose();
            }
        }

        #endregion




        public List<CustomerMTS> FindPagedCustomerMTS(int pageIndex, int pageCount)
        {
            if (pageIndex < 0)
                throw new ArgumentException(Resources.Messages.exception_InvalidPageIndex, "pageIndex");

            if (pageCount <= 0)
                throw new ArgumentException(Resources.Messages.exception_InvalidPageCount, "pageCount");

            bool enabled = true;
            Specification<CustomerMTS> onlyEnabledSpec = new DirectSpecification<CustomerMTS>(c => c.IsEnabled == enabled);

            return _CustomerMTSRepository.GetPagedElements(pageIndex, pageCount, c => c.LastName, onlyEnabledSpec, true).ToList();

        }
    }
}
